package cn.tedu.csmall.product.config;

import cn.tedu.csmall.product.interceptor.mybaits.InsertUpdateTimeInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * Mybatis的配置类
 *
 * @author liuweitao
 * @version 1.0.0
 */
/*取代以前@mapper标注Mapper接口
* 扫具体包下的类和接口，并加mapper注解*/
@Configuration
@MapperScan("cn.tedu.csmall.product.mapper")
public class MybatisConfiguration {

    public MybatisConfiguration(){
        System.out.println("创建配置类：MybatisConfiguration");
    }

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @PostConstruct // 使得此方法在调用了构造方法、完成了属性注入之后自动执行；获得构造方法的默认执行功能
    public void addInterceptor() {
        InsertUpdateTimeInterceptor interceptor = new InsertUpdateTimeInterceptor();
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
        }
    }
}
